# Include some external files
set( CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake )
include(PlinthMacros)

# Project settings
cmake_minimum_required( VERSION 2.8 )
cmake_policy(VERSION 2.8)
project( PLINTH )
set( CMAKE_INSTALL_PREFIX "${PROJECT_BINARY_DIR}/install" )
#set( EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/dist )

# Fixes for various platform specific issues
set( CMAKE_OSX_ARCHITECTURES "x86_64" )
#set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} )
#set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${EXECUTABLE_OUTPUT_PATH} )
#set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${EXECUTABLE_OUTPUT_PATH} ) 

# Add packaged lib directories to search path
set( EXT_LIBS "" )
if( _WIN32 )
	# For a windows install, copy all DLLs to root directory
	set( CMAKE_PREFIX_PATH ${CMAKE_SOURCE_DIR}/lib/generic ${CMAKE_SOURCE_DIR}/lib/win )
	file( GLOB_RECURSE EXT_LIBS "${CMAKE_SOURCE_DIR}" "./lib/win/*.dll" )

	# Copy external library dependencies to distribution directory
#	copy_if_different(
#		    "${CMAKE_SOURCE_DIR}/lib" "${EXECUTABLE_OUTPUT_PATH}"
#		    "${EXT_LIBS}" EXT_LIB_TGTS "External libraries"
#	)
else ()
	# For *nix installations, copy to a nested lib directory (which is added to rpath)
	if( __APPLE__ )
		# 32- or 64-bit OSX
		set( CMAKE_PREFIX_PATH ${CMAKE_SOURCE_DIR}/lib/generic ${CMAKE_SOURCE_DIR}/lib/mac )
    		file( GLOB_RECURSE EXT_LIBS "${CMAKE_SOURCE_DIR}" "lib/mac/*" )
	elseif( CMAKE_SIZEOF_VOID_P MATCHES 8 )
		# 64-bit Linux
		set( CMAKE_PREFIX_PATH ${CMAKE_SOURCE_DIR}/lib/generic ${CMAKE_SOURCE_DIR}/lib/linux_x64 )
		file( GLOB_RECURSE EXT_LIBS "${CMAKE_SOURCE_DIR}" "lib/linux_x64/*" )
	else ()
		# 32-bit Linux
		set( CMAKE_PREFIX_PATH ${CMAKE_SOURCE_DIR}/lib/generic ${CMAKE_SOURCE_DIR}/lib/linux_x86 )
		file( GLOB_RECURSE EXT_LIBS "${CMAKE_SOURCE_DIR}" "lib/linux_x86/*" )
	endif()

	# Copy external library dependencies to distribution directory
#	copy_if_different(
#		    "${CMAKE_SOURCE_DIR}/lib" "${EXECUTABLE_OUTPUT_PATH}/lib"
#		    "${EXT_LIBS}" EXT_LIB_TGTS "External libraries"
#	)
endif()

# Create a target to trigger copying of dependencies
add_custom_target( external_libraries ALL DEPENDS ${EXT_LIB_TGTS} )

# Add the main source directory as an include path
include_directories( ${CMAKE_SOURCE_DIR}/src )

# Add the Plinth modules (and possibly the default game)
add_subdirectory( ext )
add_subdirectory( src )

# Add Plinth units tests 
enable_testing()
add_subdirectory( test )
